package cn.edu.gcu.giftems.service;

import cn.edu.gcu.giftems.daos.TypeDao;
import cn.edu.gcu.giftems.pojos.Type;
import cn.edu.gcu.giftems.utils.PageUtils;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * TODO: Description here...
 *
 * @author ZhengHQ
 * @create 2018-09-24
 * @see
 */
@Transactional
public class TypeService {

    private TypeDao typeDao = new TypeDao();

    public TypeDao getTypeDao() {
        return typeDao;
    }

    public void setTypeDao(TypeDao typeDao) {
        this.typeDao = typeDao;
    }

    public void save(Type type) {
        typeDao.save(type);
    }

    //根据类型编号查询类型信息
    public Type findById(int id) {
        return typeDao.finById(id);
    }

    //更新类型信息
    public void update(Type type) {
        typeDao.update(type);
    }

    //根据类型id删除类型
    public void delete(Type type) {
        typeDao.delete(type);
    }

    public int findCount() {
        return typeDao.findCount();
    }

    //分页查询
    public PageUtils<Type> findByPage(int currPage) {
        PageUtils<Type> PageUtils = new PageUtils<Type>();
        //封装当前的页数
        PageUtils.setCurrPage(currPage);
        //封装每页记录数
        int pageSize = 10;
        PageUtils.setPageSize(pageSize);
        //封装总记录数
        int totalCount = typeDao.findCount();
        PageUtils.setTotalCount(totalCount);
        //封装总页数
        int totalPage = totalCount%pageSize == 0?totalCount/pageSize:(totalCount/pageSize+1);
        PageUtils.setTotalPage(totalPage);
        //封装每页显示的数据
        int begin = (currPage - 1)*pageSize;
        List<Type> list = typeDao.findByPage(begin,pageSize);
        PageUtils.setList(list);
        return PageUtils;
    }


    public List<Type> findAll() {
        return typeDao.findAll();
    }

    public PageUtils<Type> findByParameter(Integer currPage, String filter_name, String simple_filter_value) {
        PageUtils<Type> PageUtils = new PageUtils<Type>();
        PageUtils.setCurrPage(currPage);
        PageUtils.setTotalPage(1);
        List<Type> list = typeDao.findByParameter(filter_name,simple_filter_value);
        PageUtils.setList(list);
        int totalCount = list.size();
        PageUtils.setTotalCount(totalCount);
        return PageUtils;
    }
}
